package org.easy.core.util;

import java.awt.*;

public class ColorMatcherUtil {


    // 将HEX颜色转换为Lab颜色
    public static double[] hexToLab(String hex) {
        Color color = Color.decode(hex);
        float[] lab = rgbToLab(color.getRed(), color.getGreen(), color.getBlue());
        return new double[]{lab[0], lab[1], lab[2]};
    }

    // RGB到Lab的转换
    public static float[] rgbToLab(int r, int g, int b) {
        float[] rgb = {r / 255f, g / 255f, b / 255f};

        // 归一化到XYZ空间
        float[] xyz = rgbToXyz(rgb);

        // XYZ到Lab的转换
        return xyzToLab(xyz);
    }

    // RGB到XYZ的转换
    public static float[] rgbToXyz(float[] rgb) {
        // RGB到XYZ的矩阵转换
        float r = (rgb[0] > 0.04045f) ? (float) Math.pow((rgb[0] + 0.055f) / 1.055f, 2.4f) : rgb[0] / 12.92f;
        float g = (rgb[1] > 0.04045f) ? (float) Math.pow((rgb[1] + 0.055f) / 1.055f, 2.4f) : rgb[1] / 12.92f;
        float b = (rgb[2] > 0.04045f) ? (float) Math.pow((rgb[2] + 0.055f) / 1.055, 2.4f) : rgb[2] / 12.92f;

        r = r * 100;
        g = g * 100;
        b = b * 100;

        float x = r * 0.4124f + g * 0.3576f + b * 0.1805f;
        float y = r * 0.2126f + g * 0.7152f + b * 0.0722f;
        float z = r * 0.0193f + g * 0.1192f + b * 0.9505f;

        return new float[]{x, y, z};
    }

    // XYZ到Lab的转换
    public static float[] xyzToLab(float[] xyz) {
        // 参照白点 D65
        float x = xyz[0] / 95.047f;
        float y = xyz[1] / 100.000f;
        float z = xyz[2] / 108.883f;

        // 将XYZ值归一化
        x = (x > 0.008856f) ? (float) Math.pow(x, 1 / 3.0f) : (0.12842f * x) + (16 / 116.0f);
        y = (y > 0.008856f) ? (float) Math.pow(y, 1 / 3.0f) : (0.12842f * y) + (16 / 116.0f);
        z = (z > 0.008856f) ? (float) Math.pow(z, 1 / 3.0f) : (0.12842f * z) + (16 / 116.0f);

        float l = (116 * y) - 16;
        float a = 500 * (x - y);
        float b = 200 * (y - z);

        return new float[]{l, a, b};
    }

    // 计算Lab颜色空间的距离（CIE76）
    public static double calculateLabDistance(double[] lab1, double[] lab2) {
        return Math.sqrt(Math.pow(lab1[0] - lab2[0], 2) + Math.pow(lab1[1] - lab2[1], 2) + Math.pow(lab1[2] - lab2[2], 2));
    }

    // 查找最接近的颜色
    public static String findClosestColor(String targetHex, String[] hexColors) {
        double[] targetLab = hexToLab(targetHex);
        double minDistance = Double.MAX_VALUE;
        String closestColor = null;

        for (String colorHex : hexColors) {
            double[] colorLab = hexToLab(colorHex);
            double distance = calculateLabDistance(targetLab, colorLab);
            if (distance < minDistance) {
                minDistance = distance;
                closestColor = colorHex;
            }
        }

        return closestColor;
    }

    public static void main(String[] args) {
        String[] HEX_COLORS = {
                "#99abb9",
                "#beb76b",
                "#544945",
                "#b87175",
                "#22262f",
                "#70aca2",
                "#da5a4d",
                "#c36895",
                "#c3936f",
        };
        String targetColor = "#D8C4C5";  // 目标颜色

        String closestColor = findClosestColor(targetColor, HEX_COLORS);
        System.out.println("Closest color to " + targetColor + " is " + closestColor);
    }
}
